package com.zxx.coolweather._wuguan2;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanzhenjie.nohttp.Headers;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.rest.RestRequest;
import com.yanzhenjie.nohttp.rest.StringRequest;

/**
 * 返回Json字符串
 * @link {JsonObjectRequest}
 * @author zxx on 2022/1/12
 */
public class RequestFastJson extends RestRequest<JSONObject> {

    // 默认使用GET请求方法。
    public RequestFastJson(String url) {
        this(url, RequestMethod.GET);
    }

    public RequestFastJson(String url, RequestMethod requestMethod) {
        super(url, requestMethod);

        // 设置Accept请求头，告诉服务器，我们需要application/json 所有数据。
        setAccept(Headers.HEAD_VALUE_ACCEPT_ALL);

        //JsonObjectRequest.class;
    }


    @Override
    public JSONObject parseResponse(Headers responseHeaders, byte[] responseBody) throws Exception {
        String result = StringRequest.parseResponseString(responseHeaders, responseBody);
        return JSON.parseObject(result);    // StringRequest就是少了这句话而已。
    }
}
